<template>
  <a-select
    v-model:value="selectValue"
    class="min-w-[160px]"
    placeholder="请选择"
    :options="options"
    :virtual="false"
    :field-names="{ label: 'cwh', value: 'cwh' }"
    @change="onChange"
    @focus="onFocus"
  >
    <template v-if="loading" #notFoundContent>
      <a-spin size="small" />
    </template>
  </a-select>
</template>

<script setup>
/**
 * @name:选择医生
 */
import { ref, watch, onMounted } from 'vue'
import { post } from '@/utils/request'

const props = defineProps({
  value: {
    type: String,
    default: null
  },
  areaCode: {
    type: String,
    default: ''
  },
  trigger: {
    desc: '请求数据时机init:初始化/focus:聚焦',
    type: String,
    default: 'init'
  },
  bedStatus: {
    type: String,
    desvription: '床位状态 0:空 1:已用',
    default: '0'
  }
})
const loading = ref(true)
const options = ref([])
const selectValue = ref(props.value)
const emit = defineEmits(['update:value', 'change'])
watch(
  () => props.value,
  (value) => {
    selectValue.value = value
  }
)
watch(
  () => props.areaCode,
  () => {
    getData()
  }
)

const onChange = (value, option) => {
  selectValue.value = value
  emit('update:value', value)
  emit('change', value, option)
}

// 获取床位
const getData = async () => {
  loading.value = true
  options.value = []
  const { code, data } = await post('/common/masterData/getCwInfoList', {
    bqbh: props.areaCode, //病区
    cwzt: props.bedStatus //床位状态
  })
  loading.value = false
  if (code !== 0) return
  options.value = data
}
const onFocus = () => {
  props.trigger === 'focus' && getData()
}
onMounted(() => {
  props.trigger === 'init' && getData()
})
</script>
